/**
 * 
 */
package com.application.Adapter;

import java.util.ArrayList;

import ReadWriteXML.CNeighbor;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.Activity.FrmChatPeer;
import com.application.Activity.R;
import com.application.Controller.ActivityController;

public class MyInviteChatAdapter extends ArrayAdapter<CNeighbor> {
	private final Context _context;
	private final ArrayList<CNeighbor> _listNeighbor;
	int _resource;
	private CheckBox _ckinvite;

	/**
	 * Constructor
	 * @param context
	 * @param textViewResourceId
	 * @param chat
	 */
	public MyInviteChatAdapter(Context context, int textViewResourceId,ArrayList<CNeighbor> neighbor){
		super(context,textViewResourceId,neighbor);
		this._context = context;
		_resource=textViewResourceId;
		_listNeighbor=neighbor;
	}

	/**
	 * 
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent){

		final CNeighbor viewNeighbor = _listNeighbor.get(position);
		

		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
			rowView = inflater.inflate(R.layout.listrow_invitechat,null, true);
		}
		if(viewNeighbor !=null)
		{
			//holder = new ViewChatList();
			LinearLayout llocontent = (LinearLayout)rowView.findViewById(R.id.invitechat_llocontent);
			TextView txtfullname = (TextView) rowView.findViewById(R.id.rowinvite_fullname);
			TextView txtusername = (TextView) rowView.findViewById(R.id.rowinvite_username);
			ImageView imguser = (ImageView) rowView.findViewById(R.id.rowinvite_img);
			_ckinvite = (CheckBox) rowView.findViewById(R.id.rowinvite_ckinvite);

			txtfullname.setText(viewNeighbor.get_member().get_fullName().toString());
			txtusername.setText(viewNeighbor.get_member().get_userName().toString());

			if (position %2 ==0) {

				imguser.setImageResource(R.drawable.img_user);
			} else {
				imguser.setImageResource(R.drawable.img_user2);
			}

			llocontent.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(ActivityController.getPeerActivity().getBaseContext(), FrmChatPeer.class);
					intent.putExtra("username", viewNeighbor.get_member().get_userName());
					intent.putExtra("fullname",  viewNeighbor.get_member().get_fullName());
					intent.putExtra("IP", viewNeighbor.get_member().get_ip());
					ActivityController.getPeerActivity().startActivity(intent);
				}
			});

			_ckinvite.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean ischeck) {
					// TODO Auto-generated method stub
					if(ischeck==true){
						viewNeighbor.set_isinvite(true);
						Log.d("PEERDROID", "ischeck = true");
					}else{
						viewNeighbor.set_isinvite(false);
						Log.d("PEERDROID", "ischeck = false");
					}
				}
			});

			imguser.setOnClickListener(new OnClickListener() {
			
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast toast = Toast.makeText(ActivityController.getPeerActivity(), " click hinh", Toast.LENGTH_SHORT);
					toast.show();
//					Intent intent = new Intent(ActivityController.getPeerActivity().getBaseContext(), FrmProfile.class);
//					intent.putExtra("username", viewNeighbor.get_member().get_userName());
//					intent.putExtra("fullname",  viewNeighbor.get_member().get_fullName());
//					intent.putExtra("IP", viewNeighbor.get_member().get_ip());
//					ActivityController.getPeerActivity().startActivity(intent);
					
				}
			});
			if(_ckinvite.isChecked()){
				viewNeighbor.set_isinvite(true);
				Log.d("PEERDROID", "ischeck = true");
			}
			if(!_ckinvite.isChecked()){
				viewNeighbor.set_isinvite(false);
				Log.d("PEERDROID", "ischeck = false");
			}

		}
		return rowView;		
	}


}
